<template>
	<view class="container">	
		<!-- 头部轮播 -->
		<view class="carousel-section">
			<!-- 背景色区域 -->
			<swiper class="carousel" circular @change="swiperChange" autoplay="true">
				<swiper-item v-for="(item, index) in carousels" :key="index" class="carousel-item">
					<image :src="item.url" />
				</swiper-item>
			</swiper>
			<!-- 自定义swiper指示器 -->
			<view class="swiper-dots">
				<text class="num">{{swiperCurrent+1}}</text>
				<text class="sign">/</text>
				<text class="num">{{swiperLength}}</text>
			</view>
		</view>
		<view class="cate-section">
			<noticeSwiper :list="notices"></noticeSwiper>
		</view>
		<scroll-view class="scroll-view-market" scroll-x="true">
			<view class="market-item" v-for="(item, i) in topSymbols" :key="item.symbol" @click="navTo(`/pages/public/kline?symbol=${item.symbol}`)">
				<view class="item">
					<view class="t">{{item.title}}<text :class="topMakretMap[`market.${item.symbol}.detail`].change | formatChangeCls">{{topMakretMap[`market.${item.symbol}.detail`].change | formatChange}}</text></view>
					<text class="c" :class="topMakretMap[`market.${item.symbol}.detail`].change | formatChangeCls">{{topMakretMap[`market.${item.symbol}.detail`].tick ? topMakretMap[`market.${item.symbol}.detail`].tick.close : '0.00'}}</text>
					<text class="b">≈ {{topMakretMap[`market.${item.symbol}.detail`].cny}} CNY</text>
				</view>
			</view>
		</scroll-view>
		<view class="menu">
			<view class="fiat" @click="navTo('/pages/otc/otc')">
				<image src="../../static/icon-fiat.png" mode="widthFix"></image>
				<view class="label">
					<text>法币交易</text>
					<text class="sub">支持ETH、USDT</text>
				</view>
			</view>
			<view class="ex">
				<view class="item exchange" @click="navTo('/pages/exchange/index', true)">
					<image src="../../static/exchange.png"></image>
					<text>快捷闪兑</text>
				</view>
				<view class="item shop" @click="navTo('/pages/shop/index')">
					<image class="miner" src="../../static/icon-shop.png"></image>
					<text>Fex商城</text>
				</view>
			</view>
		</view>
		<view class="advert">
			<uni-swiper-dot :current="current" :mode="mode">
			    <swiper class="swiper-box" autoplay="true">
			        <swiper-item v-for="(item, index) in ads" :key="index" class="swiper-item">
			        	<image :src="item.url" mode="widthFix"/>
			        </swiper-item>
			    </swiper>
			</uni-swiper-dot>
		</view>
		<!-- 市值排行 -->
		<view class="coin-section m-t">
			<view class="s-header">
				<view class="col">币种/流通市值</view>
				<view class="col r">全球指数</view>
				<view class="col r">24小时涨幅</view>
			</view>
			<view class="s-row little-line" v-for="(item, i) in markets" :key="item.symbol">
				<view class="col light">
					<image :src="item.icon" class="coinLogo"></image>
					{{item.symbol}}
					<view class="subtitle">{{item.marketcap | formatMarketcap}}</view>
				</view>
				<view class="col r light">
					{{item.priceCny}}
					<view class="subtitle">${{item.priceUsd}}</view>
				</view>
				<view class="col r"><uni-tag :text="item.changePercent | formatChange" :type="item.changePercent >= 0 ? 'success' : 'error'"></uni-tag></view>
			</view>
		</view>
		
		
	</view>
</template>

<script>
	import {
		mapState,
		mapActions
	} from 'vuex'
	import {formatUnit} from '../../utils/number'
	import {uniNoticeBar, uniTag, uniSwiperDot} from '@dcloudio/uni-ui'
	import noticeSwiper from '../../components/noticeSwiper.vue'
	import {commonMixin} from '@/common/mixin/mixin.js'
	export default {
		components: {uniNoticeBar, uniTag, noticeSwiper, uniSwiperDot},
		mixins: [commonMixin],
		data() {
			return {
				markets: [],
				notices: [],
				titleNViewBackground: '',
				swiperCurrent: 0,
				swiperLength: 0,
				carousels: [],
				ads: [],
				current: 0,
				mode: 'round' ,
				topSymbols: [
					{symbol: 'btcusdt', title: 'BTC/USDT'},
					{symbol: 'ethusdt', title: 'ETH/USDT'},
					{symbol: 'eosusdt', title: 'EOS/USDT'}
				],
				topMakretMap: {
					'market.btcusdt.detail': {},
					'market.ethusdt.detail': {},
					'market.eosusdt.detail': {}
				}
			};
		},
		filters:{
			formatChange(v){
				return (v > 0 ? '+' : '') + parseFloat(v).toFixed(2) + '%'
			},
			formatChangeCls(v){
				if(v == 0){
					return ''
				} else if(v > 0){
					return 'upper-text'
				} else {
					return 'lower-text'
				}
			},
			formatMarketcap(v){
				return formatUnit(v);
			}
		},
		onShow() {
			
		},
		onLoad() {
			this.getMaketList()
			setInterval(() =>{
				this.getMaketList()
			}, 3000)
			setTimeout(() =>{
				this.loadTopMarket()
			}, 500)
			this.loadData();
			this.notices = ["国际站4月1日14:00开放MDC/USDT交易市场", "国际站4月2日10:00上线HKL", "关于国际站即将上线 GCCT（Global Cash Coin)"];
		},
		onUnload() {
			for(let i = 0; i < 3; i++){
				let ch = `market.${this.topSymbols[i].symbol}.detail`
				let data = {
				  "unsub": ch,
				  "id": Date.now() + ""
				}
				this.$store.dispatch('WEBSOCKET_SEND', JSON.stringify(data))
				uni.$off(ch, (res) => {})
			}
		},
		methods: {
			...mapActions('common', ['marketList', 'adList', 'noticeList']),
			loadTopMarket(){
				let $this = this
				for(let i = 0; i < 3; i++){
					let ch = `market.${this.topSymbols[i].symbol}.detail`
					let data = {
					  "sub": ch,
					  "id": Date.now() + ""
					}
					this.$store.dispatch('WEBSOCKET_SEND', JSON.stringify(data))
					uni.$on(ch, (res) => {
						let d = res.data
						d.tick.close = d.tick.close.toFixed(2)
						d.change = parseFloat((d.tick.close - d.tick.open) / d.tick.open * 100).toFixed(2);
						d.cny = parseFloat(d.tick.close * 7.04).toFixed(2)
						$this.topMakretMap[res.data.ch] = d
					})
				}
			},
			async loadData() {
				this.adList().then(res =>{
					let casrousels = res.data.casrousels
					this.swiperLength = casrousels.length
					this.carousels = casrousels
					this.ads = res.data.ads
				})
				this.noticeList().then(res =>{
					this.notices = res.rows
				})
			},
			getMaketList(){
				this.marketList().then(res =>{
					this.markets = res.data
				})
			},
			//轮播图切换修改背景色
			swiperChange(e) {
				const index = e.detail.current;
				this.swiperCurrent = index;
			}
		},
		// #ifndef MP
		// 标题栏input搜索框点击
		onNavigationBarSearchInputClicked: async function(e) {
			this.$api.msg('点击了搜索框');
		},
		//点击导航栏 buttons 时触发
		onNavigationBarButtonTap(e) {
			const index = e.index;
			if (index === 0) {
				this.$api.msg('点击了扫描');
			} else if (index === 1) {
				// #ifdef APP-PLUS
				const pages = getCurrentPages();
				const page = pages[pages.length - 1];
				const currentWebview = page.$getAppWebview();
				currentWebview.hideTitleNViewButtonRedDot({
					index
				});
				// #endif
				uni.navigateTo({
					url: '/pages/notice/notice'
				})
			}
		}
		// #endif
	}
</script>

<style lang="scss">
	page {
		background: #ffffff;
		padding-top: 50upx;
	}
	.m-t{
		margin-top: 16upx;
	}
	.advert{
		padding: 0;
		.swiper-box{
			width: 100%;
			height: 230upx;
		}
		.swiper-item{
			padding: 0;
			image{
				width: 100%;
			}
		}
	}
	.scroll-view-market {
		width: 100%;
	}
	.market-item {
		display: inline-block;
		width: 33%;
		.item{
			padding: 30upx 0 30upx 0;
			width: 100%;
			display: flex;
			flex-direction: column;
			justify-content: center;
			text-align: center;
			.t{
				font-weight: bold;
				font-size: $font-sm;
			}
			.c{
				padding: 10upx 0 10upx 0;
				font-size: $font-xl;
				font-weight: bold;
			}
			.b{
				font-size: $font-sm;
				color: $font-color-disabled;
			}
		}
	}
	.menu{
		padding: 20upx 0upx;
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		font-size: $font-base;
		font-weight: bold;
		background: $uni-color-gap;
		.fiat{
			display: flex;
			flex-direction: row;
			flex: 1;
			align-items: center;
			background: #ffffff;
			padding-left: 30upx;
			margin-right: 10upx;
			.label{
				display: flex;
				flex-direction: column;
				padding-left: 20upx;
			}
			.sub{
				font-size: $font-sm;
				font-weight: normal;
			}
			image{
				width: 100upx;
			}
			text{
				font-size: $font-md;
			}
		}
		.ex{
			display: flex;
			flex-direction: column;
			flex: 1;
			.item{
				width: 100%;
				height: 100upx;
				line-height: 100upx;
				background: #ffffff;
				align-items: center;
				text-align: center;
				vertical-align: middle;
			}
			.shop{
				margin-top: 10upx;
			}
			image{
				vertical-align: middle;
				width: 50upx;
				height: 55upx;
				margin-right: 20upx;
			}
			.miner{
				width: 45upx;
				height: 45upx;
			}
		}
	}
	/* 头部 轮播图 */
	.carousel-section {
		position: relative;
		padding-top: 10px;
		background: #ffffff;
		.titleNview-placing {
			height: var(--status-bar-height);
			padding-top: 44px;
			box-sizing: content-box;
		}

		.titleNview-background {
			position: absolute;
			top: 0;
			left: 0;
			width: 100%;
			height: 426upx;
			transition: .4s;
		}
	}
	.carousel {
		width: 100%;
		height: 350upx;

		.carousel-item {
			width: 100%;
			height: 100%;
			padding: 0 28upx;
			overflow: hidden;
		}

		image {
			width: 100%;
			height: 100%;
			border-radius: 10upx;
		}
	}
	.swiper-dots {
		display: flex;
		position: absolute;
		left: 60upx;
		bottom: 15upx;
		width: 72upx;
		height: 36upx;
		background-image: url();
		background-size: 100% 100%;

		.num {
			width: 36upx;
			height: 36upx;
			border-radius: 50px;
			font-size: 24upx;
			color: #fff;
			text-align: center;
			line-height: 36upx;
		}

		.sign {
			position: absolute;
			top: 0;
			left: 50%;
			line-height: 36upx;
			font-size: 12upx;
			color: #fff;
			transform: translateX(-50%);
		}
	}
	/* 公告 */
	.cate-section {
		padding: 20upx 22upx 20upx 22upx;
		background: #fff;
	}
	/* 市值排行 */
	.coin-section{
		padding: 4upx 30upx 24upx;
		background: #fff;
		.s-header{
			display:flex;
			align-items:center;
			height: 30upx;
			line-height: 30upx;
			padding-top: 30upx;
			padding-bottom: 30upx;
			.col{
				font-size: $font-base;
				color: $font-color-light;
				flex:1;
			}
			.r{
				text-align: right;
			}
		}
		.s-row{
			display:flex;
			align-items:center;
			height: 120upx;
			.subtitle{
				font-size: $font-sm;
				font-weight: normal;
				color: $font-color-light;
				padding: 4upx 0 10upx 0;
			}
			.uni-tag--success {
			    color: #fff;
			    background-color: $uni-color-upper;
			    border-width: 0.5px;
			    border-style: solid;
			    border-color: $uni-color-upper;
				width: 160upx;
				float: right;
			}
			.uni-tag--error {
			    color: #fff;
			    background-color: $uni-color-lower;
			    border-width: 0.5px;
			    border-style: solid;
			    border-color: $uni-color-lower;
				width: 160upx;
				float: right;
			}
			.col{
				font-size: $font-base;
				color: $font-color-dark;
				flex:1;
			}
			.coinLogo {
			    width: 36upx;
				height: 36upx;
			    margin-right: 8px;
			    display: inline-block;
			    vertical-align: middle;
			    float: left;
			}
			.light{
				font-weight: bold;
				font-size: $font-lg;
				color: $font-color-dark;
			}
			.r{
				text-align: right;
			}
		}
	}
	
	.f-header{
		display:flex;
		align-items:center;
		height: 140upx;
		padding: 6upx 30upx 8upx;
		background: #fff;
		image{
			flex-shrink: 0;
			width: 80upx;
			height: 80upx;
			margin-right: 20upx;
		}
		.tit-box{
			flex: 1;
			display: flex;
			flex-direction: column;
		}
		.tit{
			font-size: $font-lg +2upx;
			color: #font-color-dark;
			line-height: 1.3;
		}
		.tit2{
			font-size: $font-sm;
			color: $font-color-light;
		}
		.icon-you{
			font-size: $font-lg +2upx;
			color: $font-color-light;
		}
	}
	/* 猜你喜欢 */
	.guess-section{
		display:flex;
		flex-wrap:wrap;
		padding: 0 30upx;
		background: #fff;
		.guess-item{
			display:flex;
			flex-direction: column;
			width: 48%;
			padding-bottom: 40upx;
			&:nth-child(2n+1){
				margin-right: 4%;
			}
		}
		.image-wrapper{
			width: 100%;
			height: 330upx;
			border-radius: 3px;
			overflow: hidden;
			image{
				width: 100%;
				height: 100%;
				opacity: 1;
			}
		}
		.title{
			font-size: $font-lg;
			color: $font-color-dark;
			line-height: 80upx;
		}
		.price{
			font-size: $font-lg;
			color: $uni-color-primary;
			line-height: 1;
		}
	}
	

</style>
